<?php
require_once dirname(__FILE__) . '/InterfaceBase.php';
class QuanquanInterface extends InterfaceBase implements IService
{
    # 应用类型 0:IGW, 1:IBW
    const QUANQUAN_WIDGET_TYPE_IGW = 0;
    const QUANQUAN_WIDGET_TYPE_IBW = 1;
    # widget列表， 1:快速双，定制联调环境widget，2:正式环境
    const WIDGET_LIST_CUSTOMIZE_PRE =1;
    const WIDGET_LIST_ONLINE =2;
    public function __construct ($env = self::APP_ENV_TEST)
    {
        parent::__construct($env);
        $this->loadSdk("SDOServiceQuanquan");
    }
    /**
     * 开通圈圈服务
     * @param array $message array("errNo"=>错误码, "errMsg"=>错误信息)
     * @param array $args array(
     * appId=>应用ID,
     * widgetType=>widget类型,
     * sp=>array(appName=>应用名称, principal=>负责人, cellPhone=>手机, telephone=>座机, 
     * email=>电子邮件, "manager"=>大客户经理姓名, "developeEngine"=>开发引擎, "graphicMode"=>图形模式, 
     * "areaServerStruct"=>区服结构:, "loginMode"=>登陆模式, "areaGroupMode"=>游戏选区、组模式, "fullScreenMode"=>全屏模式, 
     * "areaGroupIdValid"=>初始传入区组ID是否有效, "pigment"=>色深, "websitePV"=>网站每日pv, "websiteType"=>网站类型)
     * app=>array('color'=>颜色, 'skinType'=>皮肤类型)IGW|array('domain'=>域名)IBW,
     * widget=>array(wdList=>widget列表(二维数组), auto=>是否开启转换)
     * )
     * @param int $env 环境变量
     * @return boolean
     */
    public function open ($args, $env = null)
    {
        if (! $env) {
            $env = $this->env;
        }
        $this->startLog($args, $env, __FUNCTION__, __CLASS__);
        $msg = '';
        $sdkEnv = self::$ENVType[$env];
        $appId = $args['appId'];
        $widgetType = $args['widgetType'];
        $spMethod = "AddSpInfo";
        $spInfo = $args['sp'];
        if (SDOServiceQuanquan::GetSpInfo($msg, $appId, $sdkEnv)) {
            $spMethod = "EditSpInfo";
        }
        if (! isset($spInfo['appId'])) {
            $spInfo['appId'] = $appId;
        }
        $this->processLog(__CLASS__, $spMethod);
        if (! SDOServiceQuanquan::$spMethod($msg, $spInfo, $sdkEnv)) {
            $this->setError(SDOServiceQuanquan::getErrNo(), $msg);
            $this->endLog(__CLASS__);
            return false;
        }
        $appInfo = $args['app'];
        if ($widgetType == self::QUANQUAN_WIDGET_TYPE_IBW) {
        	$this->processLog(__CLASS__, 'IbwApply');
            if (! SDOServiceQuanquan::IbwApply($msg, $appId, $appInfo['domain'], $sdkEnv)) {
                $this->setError(SDOServiceQuanquan::getErrNo(), $msg);
                $this->endLog(__CLASS__);
                return false;
            }
        } else {
        	$this->processLog(__CLASS__, 'IgwApply');
            if (! SDOServiceQuanquan::IgwApply($msg, $appId, $appInfo['color'], $appInfo['skinType'], 
            $sdkEnv)) {
                $this->setError(SDOServiceQuanquan::getErrNo(), $msg);
                $this->endLog(__CLASS__);
                return false;
            }
        }
        if (isset($args['widget'])) {
        	$this->processLog(__CLASS__, 'UpdateWidgetList');
            $widgets = $args['widget'];
            if (! SDOServiceQuanquan::UpdateWidgetList($msg, $appId, $widgets['wdList'], $widgetType, $sdkEnv, $widgets['auto'])) {
                $this->setError(SDOServiceQuanquan::getErrNo(), $msg);
                $this->endLog(__CLASS__);
                return false;
            }
        }
        $this->endLog(__CLASS__);
        return true;
    }
    /**
     * 关闭圈圈服务
     * @param array $message array("errNo"=>错误码, "errMsg"=>错误信息)
     * @param array $args array(
     * appId=>应用ID,
     * widgetType=>widget类型,
     * )
     * @param int $env 环境变量
     * @return boolean
     */
    public function close ($args, $env = null)
    {
        if (! $env) {
            $env = $this->env;
        }
        $this->startLog($args, $env, __FUNCTION__, __CLASS__);
        $msg = '';
        $sdkEnv = self::$ENVType[$env];
        $this->endLog(__CLASS__);
        return true;
    }
    /**
     * 修改圈圈服务
     * @param array $message array("errNo"=>错误码, "errMsg"=>错误信息)
     * @param array $args array(
     * appId=>应用ID,
     * widgetType=>widget类型,
     * sp=>array(appName=>应用名称, principal=>负责人, cellPhone=>手机, telephone=>座机, 
     * email=>电子邮件, "manager"=>大客户经理姓名, "developeEngine"=>开发引擎, "graphicMode"=>图形模式, 
     * "areaServerStruct"=>区服结构:, "loginMode"=>登陆模式, "areaGroupMode"=>游戏选区、组模式, "fullScreenMode"=>全屏模式, 
     * "areaGroupIdValid"=>初始传入区组ID是否有效, "pigment"=>色深, "websitePV"=>网站每日pv, "websiteType"=>网站类型)
     * app=>array('color'=>颜色, 'skinType'=>皮肤类型)IGW|array('domain'=>域名)IBW,
     * widget=>array(wdList=>widget列表(二维数组), auto=>是否开启转换)
     * )
     * @param int $env 环境变量
     * @return boolean
     */
    public function update ($args, $env = null)
    {
        if (! $env) {
            $env = $this->env;
        }
        $this->startLog($args, $env, __FUNCTION__, __CLASS__);
        $msg = '';
        $sdkEnv = self::$ENVType[$env];
        $appId = $args['appId'];
        $widgetType = $args['widgetType'];
        if (! SDOServiceQuanquan::GetSpInfo($msg, $appId, $sdkEnv)) {
            $this->setError(SDOServiceQuanquan::getErrNo(), "APP not exists");
            $this->endLog(__CLASS__);
            return false;
        }
        if (isset($args['sp'])) {
        	$this->processLog(__CLASS__, 'EditSpInfo');
            $spInfo = $args['sp'];
            if (! isset($spInfo['appId'])) {
                $spInfo['appId'] = $appId;
            }            
            if (! SDOServiceQuanquan::EditSpInfo($msg, $spInfo, $sdkEnv)) {
                $this->setError(SDOServiceQuanquan::getErrNo(), $msg);
                $this->endLog(__CLASS__);
                return false;
            }
        }
        if (isset($args['app'])) {
            $appInfo = $args['app'];
            if ($widgetType == self::QUANQUAN_WIDGET_TYPE_IBW) {
            	$this->processLog(__CLASS__, 'IbwApply');
                if (! SDOServiceQuanquan::IbwApply($msg, $appId, $appInfo['domain'], 
                $sdkEnv)) {
                    $this->setError(SDOServiceQuanquan::getErrNo(), $msg);
                    $this->endLog(__CLASS__);
                    return false;
                }
            } else {
            	$this->processLog(__CLASS__, 'IgwApply');
                if (! SDOServiceQuanquan::IgwApply($msg, $appId, $appInfo['color'], 
                $appInfo['skinType'], $sdkEnv)) {
                    $this->setError(SDOServiceQuanquan::getErrNo(), $msg);
                    $this->endLog(__CLASS__);
                    return false;
                }
            }
        }
        if (isset($args['widget'])) {
        	$this->processLog(__CLASS__, 'UpdateWidgetList');
            $widgets = $args['widget'];
            if (! SDOServiceQuanquan::UpdateWidgetList($msg, $appId, $widgets['wdList'], $widgetType, $sdkEnv, $widgets['auto'])) {
                $this->setError(SDOServiceQuanquan::getErrNo(), $msg);
                $this->endLog(__CLASS__);
                return false;
            }
        }
        $this->endLog(__CLASS__);
        return true;
    }
    /**
     * 获取圈圈服务配置信息
     * @param array $message array("errNo"=>错误码, "errMsg"=>错误信息)
     * @param array $args array(
     * appId=>应用ID,
     * widgetType=>widget类型,
     * widgetListType=>QuanquanInterface::WIDGET_LIST_CUSTOMIZE_PRE
     * @param int $env 环境变量
     * @return boolean|array('colorList'=>IGW颜色列表, 'colorType'=>皮肤列表, 'widgetList'=>widget列表)
     */
    public function get ($args, $env = null)
    {
        if (! $env) {
            $env = $this->env;
        }
        $this->startLog($args, $env, __FUNCTION__, __CLASS__);
        $msg = '';
        $sdkEnv = self::$ENVType[$env];
        $appId = $args['appId'];
        $widgetType = $args['widgetType'];
        $result = array();
        if ($widgetType==self::QUANQUAN_WIDGET_TYPE_IGW) {
        	$this->processLog(__CLASS__, 'GetConfig');
        	$tmp = SDOServiceQuanquan::GetConfig($msg, $sdkEnv);
        	if ($tmp===FALSE) {
        	    $this->setError(SDOServiceQuanquan::getErrNo(), $msg);
        	    $this->endLog(__CLASS__);
        		return false;
        	}
        	$colorList = array();
        	$colorType = array();
            if (isset($tmp[0]->RegModel->Key)) {
        		$colorType[] = (array)$tmp[0]->RegModel;
        	} else {
            	foreach($tmp[0]->RegModel as $colorObj) {
            	    $colorType[] = (array)$colorObj;
            	}
        	}
        	if (isset($tmp[1]->RegModel->Key)) {
        		$colorList[] = (array)$tmp[1]->RegModel;
        	} else {
            	foreach($tmp[1]->RegModel as $colorObj) {
            	    $colorList[] = (array)$colorObj;
            	}
        	}
        	unset($tmp);
        	$result['colorList'] = $colorList;
        	$result['colorType'] = $colorType;
        	unset($colorList);
        	unset($colorType);
        }
        if (isset($args['widgetListType'])) {
        	$widgetListType = $args['widgetListType'];
        	if ($widgetListType==self::WIDGET_LIST_CUSTOMIZE_PRE) {
        		$this->processLog(__CLASS__, 'getCustomizePreWDList');
        		$result['widgetList'] = SDOServiceQuanquan::getCustomizePreWDList($appId, $widgetType);
        	} else {
        		$this->processLog(__CLASS__, 'getDoubleOnlineWDList');
        	    $result['widgetList'] = SDOServiceQuanquan::getDoubleOnlineWDList($appId, $widgetType);
        	}
        }
        $this->endLog(__CLASS__);
        return $result;
    }
    /**
     * 同步
     * @param array $message
     * @param array $args array(
     * 'appId'=>应用ID,
     * )
     * @param mixed $env
     */
    public function sync ($args, $env = null)
    {
        if (! $env) {
            $env = $this->env;
        }
        $this->startLog($args, $env, __FUNCTION__, __CLASS__);
        $msg = '';
        $sdkEnv = self::$ENVType[$env];
        $this->endLog(__CLASS__);
        return true;
    }
}